package com.example.onlinetaxi.ui.activity.Driver.MainPage.OrderFragments;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.onlinetaxi.R;
import com.example.onlinetaxi.callback.OrderCallBack.CompletedOrderCallBack;
import com.example.onlinetaxi.callback.OrderCallBack.GetOrderCallBack;
import com.example.onlinetaxi.callback.OrderCallBack.ReceivedOrderCallBack;
import com.example.onlinetaxi.dto.OrderDto.CompletedOrderDto;
import com.example.onlinetaxi.dto.OrderDto.OrderDTO;
import com.example.onlinetaxi.dto.OrderDto.ReceivedOrderDto;
import com.example.onlinetaxi.util.OKHttpUtil.HttpUrl;
import com.example.onlinetaxi.util.OKHttpUtil.OKHttpUtil;
import com.example.onlinetaxi.view.Adapter.CancelOrderAdapter;
import com.example.onlinetaxi.view.Adapter.DriverCancelOrderAdapter;
import com.example.onlinetaxi.view.Dialog.MyDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DriverCancelOrderFragment extends Fragment {
    private View rootView;
    // 无订单提醒
    private TextView empty;
    // 订单列表
    private List<ReceivedOrderDto> orderList = new ArrayList<>();
    // 重新获取订单
    private GetOrderCallBack getOrderCallBack;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_driver_cancel_order, container, false);

        initView();

        initEvent();

        return rootView;
    }

    private void initEvent() {
        // 初始化订单列表控件
        initRecyclerView();

        // 初始化空页面
        if (orderList.size() != 0) {
            empty.setVisibility(View.INVISIBLE);
        } else {
            empty.setVisibility(View.VISIBLE);
        }
    }

    private void initView() {
        empty = rootView.findViewById(R.id.driver_cancel_order_empty);

        if (orderList.size() != 0) {
            empty.setVisibility(View.INVISIBLE);
        }
    }

    private void initRecyclerView() {
        // 找到布局文件中的recyclerView
        RecyclerView recyclerView = rootView.findViewById(R.id.driver_cancel_order_list);
        /*指定RecyclerView布局方式*/
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        /*指定为LinearLayout布局*/
        recyclerView.setLayoutManager(layoutManager);
        /*适配去管理滑动显示*/
        DriverCancelOrderAdapter adapter = new DriverCancelOrderAdapter(orderList);

        DriverCancelOrderAdapter.drawable1 = getResources().getDrawable(R.color.red);
        DriverCancelOrderAdapter.drawable2 = getResources().getDrawable(R.color.perfect_red);

        adapter.setReceivedOrderCallBack(new ReceivedOrderCallBack() {
            @Override
            public void jump(ReceivedOrderDto order) {
                // 不进行跳转
            }

            @Override
            public void clickButton(ReceivedOrderDto order) {
                // 点击按钮进行删除订单操作
                showDialog(order);
            }
        });

        /*设置适配*/
        recyclerView.setAdapter(adapter);
    }


    private void showDialog(ReceivedOrderDto order) {
        View.OnClickListener onCancelClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 取消，不操作
            }
        };

        View.OnClickListener onConfirmClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击确认
                // 删除订单
                deleteOrder(order.getId().toString());

                // 刷新当前页面
                refresh();

                Toast.makeText(getActivity(), "您已删除该订单!", Toast.LENGTH_SHORT).show();
            }
        };

        MyDialog.drawable = getContext().getDrawable(R.drawable.dialog_confirm_driver);

        // 显示提示框
        MyDialog.showInfoDialog(getActivity(), "您是否要删除当前订单记录？", "取消", onCancelClickListener, "确认", onConfirmClickListener);
    }

    // 删除订单
    private void deleteOrder(String orderId) {
        Map<String, String> map = new HashMap<>();
        map.put("orderId", orderId);

        // 发出删除请求
        OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "order", "deleteOrder");
    }

    public void setGetOrderCallBack(GetOrderCallBack getOrderCallBack) {
        this.getOrderCallBack = getOrderCallBack;
    }

    private void refresh() {
        // 重新获取order列表
        getOrderCallBack.refreshOrder();

        // 刷新当前页面
        initEvent();
    }

    public void setOrderList(List<ReceivedOrderDto> orderList) {
        this.orderList = orderList;
    }
}